﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class LogLineContentView : UIBehaviour, ILayoutElement
{
    public InputField Target;

    public float minWidth => _rectTran.sizeDelta.x;

    public float preferredWidth => _rectTran.sizeDelta.x;

    public float flexibleWidth => _rectTran.sizeDelta.x;

    public float minHeight => 0;

    public float preferredHeight => _height;

    public float flexibleHeight => _height;

    public int layoutPriority => 10;

    RectTransform _rectTran { get { return transform as RectTransform; } }
    string _s = "测试1\n测试2";
    float _height;

    public void CalculateLayoutInputHorizontal()
    {
    }

    public void CalculateLayoutInputVertical()
    {
        var text = Target.textComponent;
        TextGenerationSettings generationSettings = text.GetGenerationSettings(new Vector2(preferredWidth, 0));
        _height =  text.cachedTextGeneratorForLayout.GetPreferredHeight(_s, generationSettings) / text.pixelsPerUnit;
        Target.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _height);
        Target.textComponent.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _height);
    }

    public void SetText(string s)
    {
        _s = s;
        Target.text = _s;
    }
}
